package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;
    @Autowired
    private AlbumInfoService albumInfoService;
    @Autowired
    private VodService vodService;

    @GuiGuLogin
    @Operation(summary = "获取当前登录用户的全部专辑列表")
    @GetMapping("/albumInfo/findUserAllAlbumList")
    public Result<List<AlbumInfo>> findUserAllAlbumList() {
        //获取当前用户登录id
        Long userId = AuthContextHolder.getUserId();
        //调用业务层获取专辑列表
        List<AlbumInfo> list = albumInfoService.findUserAllAlbumList(userId);
        //返回结果
        return Result.ok(list);
    }

    @Operation(summary = "音视频上传点播平台")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, String>> uploadTrack(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }

    //todo 登录才能访问
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@Validated @RequestBody TrackInfoVo trackInfoVo) {
        //1.获取当前登录用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务层保存声音-发起审核任务
        trackInfoService.saveTrackInfo(userId, trackInfoVo);
        return Result.ok();
    }

    @Operation(summary = "条件分页查询当前用户声音列表（包含声音统计信息）")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<IPage<TrackListVo>> findUserTrackPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody TrackInfoQuery trackInfoQuery
    ) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        trackInfoQuery.setUserId(userId);
        //2.构建分页所需分页对象
        IPage<TrackListVo> pageInfo = new Page<>(page, limit);
        //3.查询业务层(持久层)获取分页数据
        pageInfo = trackInfoService.findUserTrackPage(pageInfo, trackInfoQuery);
        return Result.ok(pageInfo);
    }

    @Operation(summary = "根据声音ID查询声音信息")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * 更新声音信息
     *
     * @param id
     * @param trackInfoVo
     * @return
     */
    @Operation(summary = "更新声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @Validated @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(trackInfoVo, id);
        return Result.ok();
    }

    /**
     * 删除声音信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除声音信息")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    /**
     * 查询专辑声音分页列表
     *
     * @param albumId
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary ="查询专辑声音分页列表")
    @GuiGuLogin(required = false)
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
            @PathVariable Long albumId,
            @PathVariable Long page,
            @PathVariable Long limit) {
        //尝试获取用户id
        Long userId = AuthContextHolder.getUserId();
        //构建分页对象
        Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
        //调用业务逻辑->持久层：封装：总记录数，总页数，当前页数据
        pageInfo = trackInfoService.findAlbumTrackPage(pageInfo,albumId,userId);
        //相应分页对象
        return Result.ok(pageInfo);
    }

    /**
     * 查询声音统计信息
     * @param trackId
     * @return
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "查询声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }
}

